//
//  HNTWLViewController.m
//  Estate
//
//  Created by heyong on 16/6/13.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "HNTWLViewController.h"
#import "A_HNTSJController.h"
#import "A_HNTCCViewController.h"
#import "A_HNTRMViewController.h"
#import "A_HNTQYController.h"

@interface HNTWLViewController ()
@end

@implementation HNTWLViewController
-(NSMutableArray *)selectedAssetURLs{
    if(_selectedAssetURLs==nil){
        _selectedAssetURLs=[NSMutableArray array];
    }
    return  _selectedAssetURLs;
}
- (NSMutableDictionary *)dataDic{
    if(_dataDic==nil){
        _dataDic=[NSMutableDictionary dictionary];
    }
    return _dataDic;
}
- (NSMutableArray *)dataArr1{
    if(_dataArr1==nil){
        _dataArr1=[NSMutableArray array];
    }
    return _dataArr1;
}

- (NSMutableArray *)dataArr2{
    if(_dataArr2==nil){
        _dataArr2=[NSMutableArray array];
    }
    return _dataArr2;
}

-(UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]initWithFrame:CGRECTS(0, 0, 375, 667)];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        _tableView.bounces=NO;
        [LSUtility setExtraCellLineHidde:_tableView tabHeader:NO tabFooter:NO];
        [_tableView registerClass:[H1_ImagesCell class] forCellReuseIdentifier:kCellIdentifier_H1_ImagesCell];
    }
    return _tableView;
}

- (UIImagePickerController *)picker{
    if(!_picker){
        _picker = [[UIImagePickerController alloc] init];
        _picker.delegate = self;
        _picker.allowsEditing = NO;//设置可编辑
        _picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    return _picker;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
    [self.view addSubview:self.tableView];
    _curTweet = [[Tweet alloc] init];
    [self setData];

    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(refresh:) name:@"RefreshImage_4" object:nil];
}

- (void)refresh:(id)sender{
    if(self.selectedAssetURLs!=nil){
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            //设置图片
            self.curTweet.selectedAssetURLs =self.selectedAssetURLs;
            NSLog(@"self.curTweet.selectedAssetURLs:%@",self.curTweet.selectedAssetURLs);
            //回到主线程来刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:4]] withRowAnimation:UITableViewRowAnimationFade];
            });
        });
    }
}

#pragma mark-设置数据
-(void)setData{
    WEAKSELF
    if(_site){
        /*
         * 设计参数情况
         */
        //钢筋混凝土灌注桩(非水下混凝土)"
        NSDictionary *_dic1 =_site.wl_biaoh[@"墙"];
        if(_dic1){
            [self.dataArr1 addObject:_dic1];
            
            //钢筋混凝土灌注桩(水下混凝土)
            NSDictionary *_dic2 =_site.wl_biaoh[@"柱"];
            [self.dataArr1 addObject:_dic2];
            
            //护坡桩
            NSDictionary *_dic3 =_site.wl_biaoh[@"梁"];
            [self.dataArr1 addObject:_dic3];
            
            //CFG
            NSDictionary *_dic4 =_site.wl_biaoh[@"(底)版"];
            [self.dataArr1 addObject:_dic4];
            
            //楼梯间
            NSDictionary *_dic5 =_site.wl_biaoh[@"楼梯间"];
            [self.dataArr1 addObject:_dic5];
            
            //其他
            NSDictionary *_dic6 =_site.wl_biaoh[@"其他"];
            [self.dataArr1 addObject:_dic6];
            [weakSelf.dataDic setObject:_site.wl_biaoh forKey:@"设计混凝土标号、用量及设计坍落度情况"];
        }
      
        
        /*
         *  见证取样情况
         */
        
        NSLog(@"%@",_site.wl_jianz);
        NSDictionary *_dic1_1 =_site.wl_jianz[@"标准养护"];

        if(_dic1_1){
            [self.dataArr2 addObject:_dic1_1];
            
            NSDictionary *_dic2_1 =_site.wl_jianz[@"同条件养护"];
            [self.dataArr2 addObject:DEFISNUL(_dic2_1)];
            
            NSDictionary *_dic3_1 =_site.wl_jianz[@"拆模同条件"];
            [self.dataArr2 addObject:_dic3_1];
            
            NSDictionary *_dic4_1 =_site.wl_jianz[@"防水混凝土"];
            [self.dataArr2 addObject:_dic4_1];
            
            NSDictionary *_dic5_1 =_site.wl_jianz[@"冬施(转常温)"];
            [self.dataArr2 addObject:_dic5_1];
            
            NSDictionary *_dic6_1 =_site.wl_jianz[@"其他"];
            [self.dataArr2 addObject:_dic6_1];
            
            [weakSelf.dataDic setObject:_site.wl_jianz forKey:@"见证试块取样情况"];
            
            self.selectedAssetURLs= [LSUtility quertFileImageType:@"RECORD_02_SIDE_03" andId:_site.recordId andFilePathArr:_site.fileArray];
            [self refresh:nil];

        }
    }else{
        //默认为空 设计参数情况
        for (int i=0; i<6; i++) {
            NSMutableDictionary *dics=[NSMutableDictionary dictionary];
            [dics setObject:@"" forKey:@"设计标号"];
            [dics setObject:@"" forKey:@"设计方量m3"];
            [dics setObject:@"" forKey:@"设计坍落度mm"];
            [self.dataArr1 addObject:dics];
        }
        
        NSMutableDictionary *subDataDic1=[NSMutableDictionary dictionary];
        
        [subDataDic1 setObject:self.dataArr1[0] forKey:@"墙"];
        [subDataDic1 setObject:self.dataArr1[1] forKey:@"柱"];
        [subDataDic1 setObject:self.dataArr1[2] forKey:@"梁"];
        [subDataDic1 setObject:self.dataArr1[3] forKey:@"(底)版"];
        [subDataDic1 setObject:self.dataArr1[3] forKey:@"楼梯间"];
        [subDataDic1 setObject:self.dataArr1[4] forKey:@"其他"];
        
        [weakSelf.dataDic setObject:subDataDic1 forKey:hnt_wl_1];
        
        //默认为空 见证取样情况
        NSMutableDictionary *subDataDic2=[NSMutableDictionary dictionary];
        for (int i=0; i<6; i++) {
            NSMutableDictionary *dics=[NSMutableDictionary dictionary];
            [dics setObject:@"" forKey:@"取样组数"];
            [dics setObject:@"" forKey:@"见证取样单编号"];
            [dics setObject:@"" forKey:@"试块编号"];
            [self.dataArr2 addObject:dics];
        }
        [subDataDic2 setObject:self.dataArr2[0] forKey:@"标准养护"];
        [subDataDic2 setObject:self.dataArr2[1] forKey:@"同条件养护"];
        [subDataDic2 setObject:self.dataArr2[2] forKey:@"拆模同条件"];
         [subDataDic2 setObject:self.dataArr2[3] forKey:@"防水混凝土"];
         [subDataDic2 setObject:self.dataArr2[4] forKey:@"冬施(转常温)"];
         [subDataDic2 setObject:self.dataArr2[5] forKey:@"其他"];
        
        [weakSelf.dataDic setObject:subDataDic2 forKey:hnt_wl_4];
    }

}

#pragma mark-UITaleiViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 0;
    }else{
        return 10;
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==4) {
        return  [H1_ImagesCell cellHeightWithObj:_curTweet title:@"附件"];
    }else{
        return 44;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    long section=indexPath.section;
    if(section!=4){
        UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"aa"];
        if(cell==nil){
            cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"aa"];
        }
        cell.textLabel.font=[UIFont systemFontOfSize:14.];
        cell.textLabel.textColor=UIColorFromRGB(0x797979);
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        if(section==0){
            cell.textLabel.text=@"设计混凝土标号、用量及设计坍落度情况";
        }else if(section==1){
            cell.textLabel.text=@"抽测混凝土工作性能（坍落度)情况";
        }else if (section==2){
            cell.textLabel.text=@"入模温度检查";
        }else{
            cell.textLabel.text=@"见证试块取样情况";
        }
        
        return cell;
    }else{
        WEAKSELF
        H1_ImagesCell *cells = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier_H1_ImagesCell forIndexPath:indexPath];
        [cells setTitle:@"附件"];
        cells.isEdit=self.isEdit;
        cells.curTweet = _curTweet;
        cells.addPicturesBlock = ^(){
            [weakSelf showActionForPhoto];
        };
        __block H1_ImagesCell *cellblock = cells;
        cells.deleteTweetImageBlockUrl = ^(TweetImage *toDelete,NSString *deurl){
            [cellblock.curTweet.tweetImages removeObject:toDelete];
            [weakSelf.selectedAssetURLs removeObject:deurl];
            [weakSelf refresh:nil];
        };
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        return cells;
    }
    
}


#pragma mark UIActionSheet M

- (void)showActionForPhoto{
    //    @weakify(self);
    [[UIActionSheet bk_actionSheetCustomWithTitle:nil buttonTitles:@[@"拍照", @"从相册选择"] destructiveTitle:nil cancelTitle:@"取消" andDidDismissBlock:^(UIActionSheet *sheet, NSInteger index) {
        
        [self photoActionSheet:sheet DismissWithButtonIndex:index];
        
    }] showInView:self.view];
}

- (void)photoActionSheet:(UIActionSheet *)sheet DismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        //拍照
        if (_curTweet.tweetImages.count >= 9) {//照片最多这么多
            kTipAlert(@"最多只可选择9张照片，已经选满了。先去掉一张照片再拍照呗～");
            return;
        }
      
        [self presentViewController:self.picker animated:YES completion:nil];//进入照相界面
    }else if (buttonIndex == 1){
        //相册
        PictureViewController *picCtr=[[PictureViewController alloc]init];
        picCtr.flag=@"file";
        NSMutableArray *array = [self.selectedAssetURLs valueForKeyPath:@"@distinctUnionOfObjects.self"];
        picCtr.selectedImages=[[NSMutableArray alloc] initWithArray:array];
        picCtr.selectedImageBlock=^(NSMutableArray *array){
            for (NSString *path in array) {
                //添加图片路径到已选数组
                [self.selectedAssetURLs addObject:path];
            }
        };
        [self.navigationController pushViewController:picCtr animated:YES];
    }
}

#pragma mark UIImagePickerControllerDelegate

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image=[info objectForKey:UIImagePickerControllerOriginalImage];
    self.curTweet.editImageView = [[EditImageView alloc]initWithImage:image];
    WEAKSELF
    self.curTweet.editImageView.completeBlock = ^(NSString *path) {
        //添加选择附件
        [weakSelf.curTweet addASelectedAssetURL:path];
        [weakSelf.selectedAssetURLs addObject:path];
        //刷新指定行
        [weakSelf.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:0 inSection:4]] withRowAnimation:UITableViewRowAnimationFade];
    };
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//!!!!:点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WEAKSELF
    long section=indexPath.section;
    if(section==0){
        //设计混泥土标号
        A_HNTSJController *sjCtr=[[A_HNTSJController alloc]init];
        sjCtr.dataArr=weakSelf.dataArr1;
        sjCtr.isEdit=self.isEdit;
        //回调得到修改 数据模型
        sjCtr.dataBlock=^(NSMutableArray *arr){
            NSMutableDictionary *subDataDic=[NSMutableDictionary dictionary];
            [subDataDic setObject:arr[0] forKey:@"墙"];
            [subDataDic setObject:arr[1] forKey:@"柱"];
            [subDataDic setObject:arr[2] forKey:@"梁"];
            [subDataDic setObject:arr[3] forKey:@"(底)版"];
            [subDataDic setObject:arr[4] forKey:@"楼梯间"];
            [subDataDic setObject:arr[5] forKey:@"其他"];
            [weakSelf.dataDic setObject:subDataDic forKey:@"混凝土灌注桩的设计参数情况"];
        };

        
        [self.navigationController pushViewController:sjCtr animated:YES];
    }else if (section==1){
        //抽测混泥土工作性能
        A_HNTCCViewController *ccCtr=[[A_HNTCCViewController alloc]init];
        
        ccCtr.site=_site;
        ccCtr.isEdit=self.isEdit;
        ccCtr.dataBlock=^(NSDictionary *dic){
            if(_site==nil){
                //新增回填土 保存。再次进入，值不会清空
                _site=[[Site_04 alloc]init];
            }
            //得到所有key 根据判断是否显示
            for (NSString *key in [dic allKeys]) {
                [dic setValue:@"是" forKey:[NSString stringWithFormat:@"%@是否显示",key]];
            }
            _site.wl_chouc=dic;
            [weakSelf.dataDic setObject:dic forKey:@"抽测混凝土工作性能（坍落度）情况"];
        };
        
        [self.navigationController pushViewController:ccCtr animated:YES];
        
    }else if (section==2){
        //入模温度检查
        A_HNTRMViewController *rmCtr=[[A_HNTRMViewController alloc]init];
        rmCtr.site=_site;
        rmCtr.isEdit=self.isEdit;
        rmCtr.dataBlock=^(NSDictionary *dic){
            if(_site==nil){
                //新增回填土 保存。再次进入，值不会清空
                _site=[[Site_04 alloc]init];
            }
            _site.wl_rum=dic;
            //得到所有key 根据判断是否显示
            for (NSString *key in [dic allKeys]) {
                [dic setValue:@"是" forKey:[NSString stringWithFormat:@"%@是否显示",key]];
            }
            
            [weakSelf.dataDic setObject:dic forKey:@"入模温度检查"];
        };
        [self.navigationController pushViewController:rmCtr animated:YES];
    }else{
        //见证试块取样情况
        A_HNTQYController *qy=[[A_HNTQYController alloc]init];
        qy.dataArr=weakSelf.dataArr2;
        qy.isEdit=self.isEdit;
        //回调得到修改 数据模型
        qy.dataBlock=^(NSMutableArray *arr){
            NSMutableDictionary *subDataDic=[NSMutableDictionary dictionary];
            [subDataDic setObject:arr[0] forKey:@"标准养护"];
            [subDataDic setObject:arr[1] forKey:@"同条件养护"];
            [subDataDic setObject:arr[2] forKey:@"拆模同条件"];
            [subDataDic setObject:arr[3] forKey:@"防水混凝土"];
            [subDataDic setObject:arr[4] forKey:@"冬施(转常温)"];
            [subDataDic setObject:arr[5] forKey:@"其他"];
            [weakSelf.dataDic setObject:subDataDic forKey:@"见证试块取样情况"];
        };
        [self.navigationController pushViewController:qy animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark scrollview
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    if (scrollView == self.tableView) {
        [self.view endEditing:YES];
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
